var _remotePlayer = null;

function RemotePlayer(table_number) {
  this.table_number = table_number;
  _remotePlayer = this;
  this.resetState();  
} 

RemotePlayer.prototype.init = function() {
  this.remoteUser = new RemoteUser(this.table_number, _controller);
}

RemotePlayer.prototype.resetState = function() {
  this.stopListeningForNextMove();
  this.inGotMovesHandler = false;
  this.intervalId = null;
  this.nextMove = 1;
  this.role = UNKNOWN;
}

RemotePlayer.prototype.startListeningForNextMove = function(moveId) {
  if (this.intervalId != null && this.nextMove == moveId) {
    return;
  }
  if (this.intervalId != null && this.nextMove != moveId) {
    this.stopListeningForNextMove();
  }
  this.nextMove = moveId;
  getRecentMoves();
  this.intervalId = window.setInterval("getRecentMoves()", 5000);
}

RemotePlayer.prototype.stopListeningForNextMove = function() {
  if (this.intervalId != null) {
    window.clearInterval(this.intervalId);
    this.intervalId = null;
  }
}
	
RemotePlayer.prototype.leaveTable = function() { //winningColor) {
    this.resetState();
    var url = "/leavetable";
    url += "?table=" + this.table_number;
    //url += "&winner=";
    //if (winningColor != NULL_COLOR) {
    //  url += COLOR_TEXT[winningColor];
    //}
    downloadUrl(url, "GET", null, onLeaveTableRequestComplete);
}

RemotePlayer.prototype.sendMove = function(move, winningColor) {
    _remotePlayer.stopListeningForNextMove();
    var url = "/game";
    url += "?table=" + this.table_number + "&moveId=" + move.number;
    if (winningColor != NULL_COLOR) {
      url += "&winner=" + COLOR_TEXT[winningColor];
    }
      
    var content = _remotePlayer.serializeMove(move);
	downloadUrl(url, "POST", "content=" + encodeURI(content), onSendMoveReturned);
}

RemotePlayer.prototype.sendNewGameRequest = function(color) {
    this.desiredColor = color;
    var url = "/newgame";
    url += "?table=" + this.table_number + "&color=" + color;
    downloadUrl(url, "GET", null, onNewGameRequest);
}

// static 	
onLeaveTableRequestComplete = function(response) {
  location.href = "/room";
}


// static
onGotRecentMoves = function(response) {
   if (_remotePlayer.inGotMovesHandler) {
     return;
   }
   _remotePlayer.inGotMovesHandler = true;
   response = parseResponse(response);
   if (response.error != "") {
     alert(response.error);
     _remotePlayer.stopListeningForNextMove();
   } else if (response.lastMoveId >= _remotePlayer.nextMove) {
     if (_remotePlayer.role == PLAY_WHITE || _remotePlayer.role == PLAY_BLACK) {
       // The check on the second line is to account for black user making a move 
       // and then refreshing. The general case doesn't work because the initial lastMoveId =1
       // This might work if we set lastMoveId = 0, but that will require re-testing everything
       if (response.lastMoveId == _remotePlayer.nextMove && 
           !(_remotePlayer.role == PLAY_BLACK && response.lastMoveId == 1)) {
         _remotePlayer.stopListeningForNextMove();
         var move = response.moves[response.lastMoveId];
         move = parseResponse(move);
         _controller.makeRemoteMove(move);
         _remotePlayer.nextMove = response.lastMoveId + 1;
       }
     }
     for (var i = _remotePlayer.nextMove; i <= response.lastMoveId; i++) {
       var move = response.moves[i];
       move = parseResponse(move);
       _controller.recreateMove(move);
       _controller.submitMove();
     }
     _remotePlayer.nextMove = response.lastMoveId + 1;
   }
   _remotePlayer.inGotMovesHandler = false;
}


// static
onSendMoveReturned = function(response) {
  response = parseResponse(response);
  if (response.error != "") {
    alert(response.error);
     _controller.showMessage(response);  
  } else {
    _remotePlayer.startListeningForNextMove(_remotePlayer.nextMove + 1);
  }
}

// static
onNewGameRequest = function(response) {
  _remotePlayer.nextMove = 1;
  response = parseResponse(response);
  if (response.status == "white") {
    _remotePlayer.role = PLAY_WHITE;
    _controller.newGame(PLAY_WHITE, response.lastMoveId);
  } else if (response.status == "black") {
    _remotePlayer.role = PLAY_BLACK;
    _controller.newGame(PLAY_BLACK, response.lastMoveId);
  } else if (response.status == "watch") {
     _remotePlayer.role = WATCH;
    _controller.newGame(WATCH);
  } else if (response.status == "wait_white") {
    _controller.showMessage("Waiting for the white player...");
    setTimeout("_remotePlayer.sendNewGameRequest(_remotePlayer.desiredColor)", 15000);
   } else if (response.status == "wait_black") {
    _controller.showMessage("Waiting for the black player...");
    setTimeout("_remotePlayer.sendNewGameRequest(_remotePlayer.desiredColor)", 15000);
  } else if (response.status == "none") {
    _controller.showMessage("Waiting for players to start game...");
    setTimeout("_remotePlayer.sendNewGameRequest(_remotePlayer.desiredColor)", 15000);
  } else {
    _controller.showMessage(response.status);
  }
  if (response.whitePlayer) {
    _controller.setWhitePlayer(response.whitePlayer);
  }
  if (response.blackPlayer) {
    _controller.setBlackPlayer(response.blackPlayer);
  }
}

// static
getRecentMoves = function() {    
    var url = "/game";
    url += "?table=" + _remotePlayer.table_number + "&moveId=" + _remotePlayer.nextMove;
	downloadUrl(url, "GET", null, onGotRecentMoves);
}

RemotePlayer.prototype.serializeMove = function(move) {
  var myJSONText = JSON.stringify(move, function (key, value) {
    return value;
  });
  return myJSONText;
}
